
function formatNumber(num) {
  return num >= 10? num: '0' + num
}

function trim(str) {
  return str.replace(/(^\s*)|(\s*$)/g, '')
}

function formatDate(time, format) {
  var date = time ? new Date(time) : new Date()
  var year = date.getFullYear()
  var month = formatNumber(date.getMonth() + 1)
  var day = formatNumber(date.getDate())
  var houre = formatNumber(date.getHours())
  var minutes = formatNumber(date.getMinutes())
  var second = formatNumber(date.getSeconds())
  format = format || 'xxxx-xx-xx xx:xx'
  format = trim(format).toLowerCase()
  if (format == 'xxxx-xx-xx xx:xx') {
    return year + '-' + month + '-' + day + ' ' + houre + ':' + minutes
  }
  if (format == 'xxxx-xx-xx') {
    return year + '-' + month + '-' + day
  }
  if (format == 'xxxx-xx-xx xx:xx:xx') {
    return year + '-' + month + '-' + day + ' ' + houre + ':' + minutes + ':' + second
  }
  if (format == 'xxxx年xx月xx日 xx:xx') {
    return year + '年' + month + '月' + day + '日 ' + houre + ':' + minutes
  }
  if (format == 'xxxx年xx月xx日 xx:xx:xx') {
    return year + '年' + month + '月' + day + '日 ' + houre + ':' + minutes + ':' + second
  }
  if (format == 'xxxx年xx月xx xx时') {
    return year + '年' + month + '月' + day + '日 ' + houre + '时'
  }
}


export {
  formatDate
}
